﻿#include "DiscWidget.h"

DiscWidget::DiscWidget(QWidget* parent
	, const Qt::WindowFlags& f): QWidget{ parent,f } {
	area = new QScrollArea(this);
	boxLayout = new QVBoxLayout(this);
	boxLayout->addWidget(area);
	area->setWidgetResizable(true);
	areaBoxLayout = new QHBoxLayout( );
	areadWidget = new QWidget(area);
	areadWidget->setLayout(areaBoxLayout);
	area->setWidget(areadWidget);
	area->setWidgetResizable(true);
	// 设置 area
	area->setWidgetResizable(true);
	area->setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow | Qt::WindowStaysOnTopHint);
}

void DiscWidget::setDiscs(QHash<QString, QHash<QString, ULARGE_INTEGER>>* discInfo) {
	static QString freeBytesAvailableKey = "可用空间";
	static QString totalNumberOfBytesKey = "总空间";
	static QString totalNumberOfFreeBytes = "剩余空间";
	if( discInfo->isEmpty( ) )
		return;
	static const char* From = "   驱动:\t\t%1\n  总空间:\t\t%2\tG\n剩余空间:\t\t%3\tG\n可用空间:\t\t%4\tG";
	// 释放 subCompont
	while( !subComponts.isEmpty( ) ) {
		QWidget* subCompont = subComponts.last( );
		areaBoxLayout->removeWidget(subCompont);
		delete subCompont;
		subComponts.removeLast( );
	}
	auto iterator = discInfo->begin( );
	auto end = discInfo->end( );
	for( ;iterator != end;++iterator ) {
		// 获取可用空间的数据
		auto sfreeBytesAvailable = iterator.value( ).value(freeBytesAvailableKey);
		long long gbFreeBytesAvailable = getGb(sfreeBytesAvailable);
		//  获取总空间数据
		auto stotalNumberOfBytesKey = iterator.value( ).value(totalNumberOfBytesKey);
		long long gbTotalNumberOfBytesKey = getGb(stotalNumberOfBytesKey);
		// 获取剩余空间数据
		auto stotalNumberOfFreeBytes = iterator.value( ).value(totalNumberOfFreeBytes);
		long long gbTotalNumberOfFreeBytes = getGb(stotalNumberOfFreeBytes);
		double value = 100 - 1.0 * gbTotalNumberOfFreeBytes / gbTotalNumberOfBytesKey * 100;
		// 创建容器
		QWidget* widget = new QWidget( );
		widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
		// 创建布局
		QVBoxLayout* boxLayout = new QVBoxLayout;
		widget->setLayout(boxLayout);
		QString title(From);
		const QString discName = iterator.key( );
		QString targetTitle = title.arg(discName).arg(gbTotalNumberOfBytesKey).arg(gbTotalNumberOfFreeBytes).arg(gbFreeBytesAvailable);
		// 创建标题
		QLabel* label = new QLabel(targetTitle, widget);
		label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
		label->setObjectName(discName);
		// 创建进度条
		QProgressBar* progressBar = new QProgressBar(widget);
		progressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
		progressBar->setRange(0, 100);
		progressBar->setOrientation(Qt::Vertical);
		progressBar->setValue(value);
		progressBar->setTextVisible(false);
		progressBar->setAlignment(Qt::AlignCenter);
		// 创建按钮
		QPushButton* pushButton = new QPushButton("复制信息", widget);
		pushButton->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
		// 设置控件
		boxLayout->addWidget(label, 2);
		boxLayout->addWidget(progressBar, 8);
		boxLayout->addWidget(pushButton, 2, Qt::AlignHCenter);
		// 存储控件
		subComponts.append(widget);
		// 拷贝
		connect(pushButton, &QPushButton::clicked, this, &DiscWidget::copyInfo);
		// 存储
		disc.insert(pushButton, iterator.key( ));
		pushButtoninfo.insert(pushButton, label);
		checkBoxsinfo.insert(label, iterator.value( ));
		// discInfo
		QHash<QString, long long> discMap = this->discInfo.value(discName);
		//static QString freeBytesAvailableKey = "可用空间";
		//static QString totalNumberOfBytesKey = "总空间";
		//static QString totalNumberOfFreeBytes = "剩余空间";
		discMap.insert(freeBytesAvailableKey, gbFreeBytesAvailable);
		discMap.insert(totalNumberOfBytesKey, gbTotalNumberOfBytesKey);
		discMap.insert(totalNumberOfFreeBytes, gbTotalNumberOfFreeBytes);
		this->discInfo.insert(discName, discMap);
		// 添加到布局
		areaBoxLayout->addWidget(widget);
	}
}

long long DiscWidget::getGb(ULARGE_INTEGER& obj) {
	return ( obj.HighPart << 2 ) + ( ( DWORD )obj.LowPart ) / 1024.0 / 1024.0 / 1024.0;
}

void DiscWidget::copyInfo(bool flage) {
	const QString  string = disc.value(( QPushButton* )sender( ), Q_NULLPTR);
	if( string == Q_NULLPTR )
		return;
	const QHash<QString, long long> hash = this->discInfo.value(string);
	if( hash.isEmpty( ) )
		return;
}
